package com.cip.master.action;

import java.util.List;
import java.util.Locale;

import com.cip.admin.model.User;
import com.cip.common.util.SecurityUtil;
import com.cip.master.model.Subject;
import com.cip.master.service.ClassYearService;
import com.cip.master.model.ClassYear;
import com.cip.master.service.SubjectService;
import com.cip.webapp.action.BaseAction;

public class SubjectAction extends BaseAction {
	
	private SubjectService  subjectService;
	private ClassYearService classYearService;
	private Subject subject;
	private List<Subject> subjectList;
	private List<ClassYear> classYears;
	private String classYear;

	public String saveOrUpdateSubject() throws Exception {
		User user = SecurityUtil.getThreadLocalUser().get();
		ClassYear classYr = classYearService.getClassYear(Integer.parseInt(classYear));
		subject.setTenantId(user.getTenantId());
		subject.setClassYear(classYr);
		subjectService.saveOrUpdateSubject(subject);
		saveMessage(getText("subject.added"));
		return SUCCESS;
	}

	public String showSubjectList() throws Exception {
		Locale locale = getRequest().getLocale();
		String subjectString = subjectService.getSubjectGridList(locale);
				
		getRequest().setAttribute("subjectGridString", subjectString);
		
//		setSubjectList(subjectService.getAllSubjectList());
		return SUCCESS;
	}

	public String showEditSubject() {
		try {
			subject = subjectService.getSubjectById(subject.getId());
			setClassYear(subject.getClassYear().getName());
			setSubject(subject);
			setClassYears(classYearService.getAllClassYear());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String showCreateSubject() throws Exception {
		setSubject(new Subject());
		setClassYears(classYearService.getAllClassYear());
		return SUCCESS;
	}

	public SubjectService getSubjectService() {
		return subjectService;
	}

	public void setSubjectService(SubjectService subjectService) {
		this.subjectService = subjectService;
	}	

	public ClassYearService getClassYearService() {
		return classYearService;
	}

	public void setClassYearService(ClassYearService classYearService) {
		this.classYearService = classYearService;
	}

	public Subject getSubject() {
		return subject;
	}

	public void setSubject(Subject subject) {
		this.subject = subject;
	}

	public List<Subject> getSubjectList() {
		return subjectList;
	}

	public void setSubjectList(List<Subject> subjectList) {
		this.subjectList = subjectList;
	}

	public List<ClassYear> getClassYears() {
		return classYears;
	}

	public void setClassYears(List<ClassYear> classYears) {
		this.classYears = classYears;
	}

	public String getClassYear() {
		return classYear;
	}

	public void setClassYear(String classYear) {
		this.classYear = classYear;
	}	
	
}